package org.nf.trading.service.app.product.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nf.trading.dao.CommentMapper;
import org.nf.trading.dao.CommentReplyMapper;
import org.nf.trading.dto.CommentReplySecondaryDTO;
import org.nf.trading.entity.Comment;
import org.nf.trading.entity.CommentReply;
import org.nf.trading.exception.app.product.ProductDataException;
import org.nf.trading.service.app.product.ProductCommentService;
import org.nf.trading.service.app.product.enums.ProductCommentEnum;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Security;
import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class ProductCommentServiceImpl implements ProductCommentService {
    private final CommentReplyMapper commentReplyMapper;

    private final CommentMapper commentMapper;
    private final RedisTemplate<String,Object> redisTemplate;

    /**
     * 添加评论
     * @param comment
     * @return
     */
    @Override
    public Comment insertComment(Comment comment) {
        if (comment == null){
            throw new ProductDataException(500,"数据为空");
        }
        int insert = commentMapper.insert(comment);
        if (insert != 1){
            throw new ProductDataException(500,"添加失败");
        }
        Comment comment1 = commentMapper.selectByPrimaryKey(comment.getCommentId());
        redisTemplate.expire(ProductCommentEnum.PREFIX.value() + comment1.getProductId(),0,TimeUnit.SECONDS);
        return comment1;
    }
    @Override
    public CommentReplySecondaryDTO addCommentReply(CommentReply commentReply) {
        log.info("回复数据：{}",commentReply);
        if (commentReply == null){
            throw new ProductDataException(500,"评论回复不能为空");
        }
        //添加评论回复
        int insert = commentReplyMapper.insert(commentReply);

        //通过评论回复id查询评论回复
        CommentReply userCommentReply = commentReplyMapper.selectByPrimaryKey(commentReply.getCommentReplyId());
        //通过上一个评论回复id查询被评论的评论回复
        CommentReplySecondaryDTO secondaryDTO = new CommentReplySecondaryDTO();
        //判断被评论回复的id是否为空，不为空则设置值
        if (commentReply.getCommentReplySecondary() == null){
            secondaryDTO.setCommentator(null);
            secondaryDTO.setCommentReplyId(commentReply.getCommentReplyId());
        }else {
            CommentReply secondaryCommentReply = commentReplyMapper.selectByPrimaryKey(commentReply.getCommentReplySecondary());
            secondaryDTO.setCommentReplyId(commentReply.getCommentReplyId());
            secondaryDTO.setCommentator(commentReplyMapper.getCommentReplyUserById(secondaryCommentReply.getCommentatorId()).getUser());
        }
        //保存评论消息
        secondaryDTO.setContent(userCommentReply.getCommentReplyContent());
        secondaryDTO.setUser(commentReplyMapper.getCommentReplyUserById(userCommentReply.getCommentatorId()).getUser());
        //更新缓存
        updateCache(commentReply.getCommentReplyId());
        return secondaryDTO;
    }

    /**
     * 设置评论缓存时间为0
     * @param commentReplyId
     */
    public void updateCache(Integer commentReplyId){
        int productId = commentReplyMapper.getProductId(commentReplyId);
//        redisTemplate.expire(ProductCommentEnum.PREFIX.value() + productId, 20, TimeUnit.MINUTES);
        redisTemplate.expire(ProductCommentEnum.PREFIX.value() + productId,0,TimeUnit.SECONDS);
        System.out.println("商品id为 = "+ productId);
    }


}
